package org.example.system.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.StudentEntity;
import org.example.system.service.StudentService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 学生控制层
 */
@RestController
@RequestMapping("/admin/student")
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 按条件分页查询
     * @param student
     * @return
     */
    @GetMapping("/list")
    public R list(StudentEntity student) {
        log.info("------------StudentController | list | input : {} ", JSON.toJSONString(student));
        IPage<StudentEntity> page = studentService.selectPage(student);
        log.info("------------StudentController | list | return : {} ", JSON.toJSONString(page));
        return R.ok(page);
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody StudentEntity student) {
        log.info("---------StudentController | add | input : {}", JSON.toJSON(student));
        int row = studentService.add(student);
        if (row <= 0) {
            return R.fail("添加失败");
        }
        return R.ok("添加成功");
    }

    /**
     * 修改
     * @param student
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody StudentEntity student) {
        log.info("---------StudentController | update | input : {}", JSON.toJSON(student));
        if (null == student.getId() || student.getId() <= 0) {
            return R.fail("参数错误:ID未传入");
        }
        int row = studentService.update(student);
        if (row <= 0) {
            return R.fail("修改失败");
        }
        return R.ok("修改成功");
    }

    /**
     * 修改密码
     * @param student
     * @return
     */
    @PutMapping("/updatePassword")
    public R updatePassword(@RequestBody StudentEntity student) {
        log.info("---------StudentController | update | input : {}", JSON.toJSON(student));
        if (null == student.getId() || student.getId() <= 0) {
            return R.fail("参数错误:ID未传入");
        }
        studentService.update(student);
        return R.ok("修改密码成功");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") Long[] ids) {
        log.info("---------StudentController | delete | input : {}", JSON.toJSON(ids));
        int row = studentService.deleteBatch(ids);
        if (row <= 0) {
            return R.fail("删除失败");
        }
        return R.ok("删除成功");
    }

    /**
     * 根据id查询学生数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable("id") Long id) {
        log.info("---------StudentController | getInfo | input : {}", JSON.toJSON(id));
        return R.ok(studentService.getOne(id));
    }

    /**
     * 根据学号查询学生数据
     * @param number
     * @return
     */
    @GetMapping("/select")
    public R getInfoBySId(@RequestParam("number") String number) {
        log.info("---------StudentController | getInfo | input : {}", JSON.toJSON(number));
        return R.ok(studentService.getOneBySId(number));
    }
}
